package cn.snorlaxzjt.LBSserver.business;


import cn.snorlaxzjt.LBSserver.common.BusinessCode;
import cn.snorlaxzjt.LBSserver.common.Converter;
import cn.snorlaxzjt.LBSserver.common.Result;
import cn.snorlaxzjt.LBSserver.po.body.FriendBody;
import cn.snorlaxzjt.LBSserver.po.entity.Friends;
import cn.snorlaxzjt.LBSserver.po.entity.User;
import cn.snorlaxzjt.LBSserver.po.page.FriendPage;
import cn.snorlaxzjt.LBSserver.po.page.PersonalInfoPage;
import cn.snorlaxzjt.LBSserver.po.page.ResultPage;
import cn.snorlaxzjt.LBSserver.service.FriendsService;
import cn.snorlaxzjt.LBSserver.service.MomentService;
import cn.snorlaxzjt.LBSserver.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class FriendBusiness {

    @Resource
    UserService userService;

    @Resource
    FriendsService friendsService;

    @Resource
    MomentService momentService;


    public FriendPage getMyFriends() {
        List<Integer> followList = friendsService.getMyFriends(userService.getCurrentUserId());
        List<Integer> fanList = friendsService.getMyFans(userService.getCurrentUserId());
        List<Integer> friendList = followList.stream().filter(fanList::contains).collect(Collectors.toList());
        List<User> followList2 = userService.getUserInfoByList(followList);
        List<User> fanList2 = userService.getUserInfoByList(fanList);
        List<User> friendList2 = userService.getUserInfoByList(friendList);
        return Converter.toFriendPage(followList2,fanList2,friendList2);
    }

    public Result<Map<String,String>> follow(Integer friendId) {
        Friends friends = new Friends();
        friends.setUserId(userService.getCurrentUserId());
        friends.setFriendId(friendId);
        friendsService.follow(friends);
        Map<String,String> res = new HashMap<>();
        res.put("friendType", getFriendType(userService.getCurrentUserId(), friendId));
        return BusinessCode.SUCCESS.toResult(res);
    }

    public Result<Map<String,String>> cancelFollow(Integer friendId) {
        friendsService.cancelFollow(userService.getCurrentUserId(), friendId);
        Map<String,String> res = new HashMap<>();
        res.put("friendType", getFriendType(userService.getCurrentUserId(), friendId));
        return BusinessCode.SUCCESS.toResult(res);
    }

    public Result<PersonalInfoPage> getPersonalInfoById(Integer id){
        PersonalInfoPage res = new PersonalInfoPage();
        User userInfo = userService.getUserById(id);
        res.setAvatar(userInfo.getAvatar());
        res.setEmail(userInfo.getEmail());
        res.setUserId(userInfo.getId());
        res.setUsername(userInfo.getUsername());
        res.setFriendType(getFriendType(userService.getCurrentUserId(),id));
        res.setMomentImgList( momentService.getRecentlyThreeImgById(id));
        return BusinessCode.SUCCESS.toResult(res);
    }

    private String getFriendType(int aid,int bid) {
        if(aid == bid)return "";
        boolean flagA = friendsService.ifFollow(aid, bid);
        boolean flagB = friendsService.ifFollow(bid, aid);
        if (flagA && flagB) {
            return "已互关";
        }
        else if (flagA) {
            return "已关注";
        }
        else if (flagB) {
            return "回关";
        }
        else {
            return "关注";
        }
    }


}
